import javax.swing.tree.TreeNode;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 28463
 * Date: 2022—07—14
 * Time: 13:37
 */

public class Demo01 {
    public class TreeNode {
         int val;
         TreeNode left;
         TreeNode right;
         TreeNode() {}
        TreeNode(int val) { this.val = val; }TreeNode(int val, TreeNode left, TreeNode right) {
         this.val = val;
         this.left = left;
         this.right = right;
     }
  }
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> ret = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while(cur != null || ! stack.empty()) {
            while(cur != null) {
                stack.push(cur);
                ret.add(cur.val);
                cur = cur.left;
            }
            TreeNode top = stack.pop();
            cur = top.right;
        }
        return ret;
    }


    public int postIndex = 0;
    private TreeNode buildTreeChild(int[] postorder,int[] inorder,int inbegin,int inend){
        //没有了左树或者没有了右树
        if(inbegin > inend) {
            return null;
        }
        TreeNode root = new TreeNode(postorder[postIndex]);
        //找到当前根节点 在中序遍历中的位置
        int rootIndex = findInorder(inorder,postorder[postIndex],inbegin,inend);
        postIndex--;
        root.right = buildTreeChild(postorder, inorder,rootIndex+1,inend);
        root.left = buildTreeChild(postorder, inorder,inbegin,rootIndex-1);
        return root;
    }

    private int findInorder(int[] inorder,int val,int inbegin,int inend){
        for(int i = inbegin; i <= inend; i++) {
            if(inorder[i] == val) {
                return i;
            }
        }
        return -1;
    }
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        postIndex = postorder.length-1;
        return buildTreeChild(postorder,inorder,0,inorder.length-1);
    }

    public int preIndex = 0;
    private TreeNode buildTreeChild(int[] preorder, int[] inorder,int inbegin,int inend){
        //没有了左树或者没有了右树
        if(inbegin > inend) {
            return null;
        }
        TreeNode root = new TreeNode(preorder[preIndex]);
        //找到当前根节点 在中序遍历中的位置
        int rootIndex = findInorder(inorder,preorder[preIndex],inbegin,inend);
        preIndex++;
        root.left = buildTreeChild(preorder, inorder,inbegin,rootIndex-1);
        root.right = buildTreeChild(preorder, inorder,rootIndex+1,inend);
        return root;
    }

    private int findInorder(int[] inorder,int val,int inbegin,int inend){
        for(int i = inbegin; i <= inend; i++) {
            if(inorder[i] == val) {
                return i;
            }
        }
        return -1;
    }
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        return buildTreeChild(preorder,inorder,0,inorder.length-1);
    }



    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null || p == null || q == null) {
            return null;
        }
        Stack<TreeNode> stack1 = new Stack<>();
        getPath(root,p,stack1);

        Stack<TreeNode> stack2 = new Stack<>();
        getPath(root,q,stack2);

        int size1 = stack1.size();
        int size2 = stack2.size();
        //两个栈个数不一样
        if(size1 > size2) {
            int tmp = size1 - size2;
            while(tmp != 0) {
                stack1.pop();
                tmp--;
            }
        }else {
            int tmp = size2 - size1;
            while(tmp != 0) {
                stack2.pop();
                tmp--;
            }
        }
        //两个栈个数一样
        while(!stack1.empty() && !stack2.empty()) {
            if(stack1.peek() == stack2.peek()) {
                return stack1.peek();
            }else {
                stack1.pop();
                stack2.pop();
            }
        }
        return null;//没有公共祖先
    }
    //找到根节点到指定节点路径上所有结点，放在栈中
    private boolean getPath(TreeNode root, TreeNode node, Stack<TreeNode> stack) {
        if(root == null || node == null) {
            return false;
        }
        stack.push(root);
        if(root == node) {
            return true;
        }
        boolean ret1 = getPath(root.left,node,stack);
        //只能说明左边没有node
        if(ret1) {
            return true;
        }
        boolean ret2 = getPath(root.right,node,stack);
        if(ret2) {
            return true;
        }
        //根节点不是   根左不是  根右也不是
        stack.pop();
        return false;
    }



    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> ret = new ArrayList<>();
        if (root == null) return ret;

        Queue<TreeNode> queue = new LinkedList<>();

        queue.offer(root);

        while(!queue.isEmpty()) {
            int size = queue.size();
            List<Integer> row = new ArrayList<>();
            while(size > 0) {
                TreeNode cur = queue.poll();
                size--;
                row.add(cur.val);
                if (cur.left != null) {
                    queue.offer(cur.left);
                }
                if (cur.right != null) {
                    queue.offer(cur.right);
                }
            }
            ret.add(row);
        }
        return ret;
    }




}
